package com.cloud.mkz.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mkz.admin.service.RbacRoleMenuService;
import com.cloud.mkz.admin.util.InitRbacUtil;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.admin.api.entity.RbacRole;
import com.cloud.mkz.admin.service.RbacRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * 权限信息
 *
 * @author mkz code generator
 * @date 2021-03-21 17:41:52
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rbac/role" )
@Api(value = "rbacrole", tags = "权限信息管理")
public class RbacRoleController {
    private final RbacRoleMenuService rbacRoleMenuService;
    private final  RbacRoleService rbacRoleService;
    private final InitRbacUtil initRbacUtil;

    /**
     * 分页查询
     * @param page 分页对象
     * @param rbacRole 权限信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getRbacRolePage(Page page, RbacRole rbacRole) {
        return ResponseResult.ok(rbacRoleService.page(page, Wrappers.query(rbacRole)));
    }


    /**
     * 通过id查询权限信息
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{pkId}" )
    public ResponseResult getById(@PathVariable("pkId" ) Long pkId) {
        return ResponseResult.ok(rbacRoleService.getById(pkId));
    }

    /**
     * 新增权限信息
     * @param rbacRole 权限信息
     * @return R
     */
    @ApiOperation(value = "新增权限信息", notes = "新增权限信息")
    @PostMapping
    public ResponseResult save(@RequestBody RbacRole rbacRole) {
        return ResponseResult.ok(rbacRoleService.save(rbacRole), "保存成功");
    }

    /**
     * 修改权限信息
     * @param rbacRole 权限信息
     * @return R
     */
    @ApiOperation(value = "修改权限信息", notes = "修改权限信息")
    @PutMapping
    public ResponseResult updateById(@RequestBody RbacRole rbacRole) {
        RbacRole role_db = rbacRoleService.getById(rbacRole.getPkId());
        if(role_db==null){
            return ResponseResult.failed("记录不存在");
        }
        role_db = getBean(role_db, rbacRole);
        return ResponseResult.ok(rbacRoleService.updateById(role_db));
    }

    /**
     * 通过id删除权限信息
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id删除权限信息", notes = "通过id删除权限信息")
    @DeleteMapping("/{pkId}" )
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult removeById(@PathVariable Long pkId) {
        rbacRoleMenuService.delteByRoleId(pkId);
        return ResponseResult.ok(rbacRoleService.removeById(pkId));
    }

    /**
     * 刷新角色权限缓存数据
     * @return ResponseResult
     */
    @ApiOperation(value = "刷新角色权限缓存数据", notes = "角色权限缓存")
    @PostMapping("/refreshCache")
    public ResponseResult refreshCache() {
        boolean refreshResult = initRbacUtil.initRbacData();
        if(!refreshResult){
            return ResponseResult.failed("刷新失败");
        }
        return ResponseResult.ok(true, "刷新成功");
    }

    public RbacRole getBean(RbacRole bean_db, RbacRole bean_form){
        bean_db.setRoleName(bean_form.getRoleName());
        bean_db.setRoleCode(bean_form.getRoleCode());
        bean_db.setRoleDesc(bean_form.getRoleDesc());
        bean_db.setOrderNo(bean_form.getOrderNo());
        bean_db.setDataScope(bean_form.getDataScope());
        bean_db.setRoleStatus(bean_form.getRoleStatus());
        return bean_db;
    }
}
